<?php
/**
 * Copyright (c) 2009, Microsoft
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Microsoft nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Microsoft ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Microsoft BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Microsoft
 * @package    WebErrorPHPToolkit
 * @subpackage BingAssets
 * @version    $Id: BingSearchAssets.php, v 1.0 2009-10-09 08:00:00Z unknown $
 * @copyright  Copyright (c) 2009, Microsoft (http://www.microsoft.com)
 * @author     jeff00seattle
 *
 */

require_once 'SiteVariables.php';
require_once 'Exception.php';


/**
 * Gather each of the Bing search assets used by Custom 404.
 *
 * @return boolean
 */
function web_error_bing_search_assets(){

  /*
   * Bing URL
   */
  $asset_name = "BingSearchAssetUrlEndpoint";
  if (web_error_get_bing_search_asset("/BrandAssets/SearchUrl", $response)){
    site_variable_set($asset_name, $response);
  } else {
    return false;
  }

  /*
   * Bing Logo
   */
  $asset_name = "BingSearchAssetLogoBigDark";
  if (web_error_get_bing_search_asset("/BrandAssets/Images/LogoBigDark", $response)){
    site_variable_set($asset_name, $response);
  } else {
    return false;
  }

  /*
   * Bing Search Icon
   */
  $asset_name = "BingSearchAssetIcon24";
  if (web_error_get_bing_search_asset("/BrandAssets/Images/Icon24", $response)){
    site_variable_set($asset_name, $response);
  } else {
    return false;
  }

  return true;
}

/**
 * Gather Bing Asset with provided XPath
 *
 * @param string $xPath XPath to find Bing Asset in returned XML Asset file.
 * @param string $asset_name Name to assign to gathered Asset value.
 * @return boolean Success status
 */
function web_error_get_bing_search_asset($xPath, &$response) {

  try {
  $response = null;

    if (function_exists("curl_init")) {
        $urlXmlBingAssetEndpoint = site_variable_get('BingSearchAssetsXmlEndpoint', false);

        if (!$urlXmlBingAssetEndpoint || strlen($urlXmlBingAssetEndpoint) == 0) {
          trigger_error("Configuration variable BingSearchAssetsXmlEndpoint not defined.", E_USER_ERROR );
          return false;
        }

        /*
         * Create a new cURL instance
         */
        $curlHandle = curl_init();

        curl_setopt ($curlHandle, CURLOPT_URL,            $urlXmlBingAssetEndpoint);
        curl_setopt ($curlHandle, CURL_HTTP_VERSION_1_1,  true);
        curl_setopt ($curlHandle, CURLOPT_HTTPGET,        true);
        curl_setopt ($curlHandle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($curlHandle, CURLOPT_CONNECTTIMEOUT, site_variable_get('Custom404CurlConnectTimeout', 0));

        if (($strCustom404CurlHttpProxy = site_variable_get('Custom404CurlHttpProxy', false))
            && !empty($strCustom404CurlHttpProxy))
        {
          curl_setopt ($curlHandle, CURLOPT_PROXY, $strCustom404CurlHttpProxy);
          if (($strCustom404CurlHttpProxyPort = site_variable_get('Custom404CurlHttpProxyPort', false))
              && !empty($strCustom404CurlHttpProxyPort))
          {
            curl_setopt ($curlHandle, CURLOPT_PROXYPORT, $strCustom404CurlHttpProxyPort);
          }
        }

        $xml_response = curl_exec($curlHandle);
        curl_close($curlHandle);
      } else {
        trigger_error('PHP cURL Library Extension php_curl.dll not available.', E_USER_ERROR);
        return false;
      }

      if ($xml_response === false && (curl_errno($curlHandle) != CURLE_OK)  ) {
      throw new WebToolkit_BingAssets_Exception('cURL error occured during request for ' . $urlXmlBingAssetEndpoint . ': ' . curl_errno($curlHandle) . ' - ' . curl_error($curlHandle));
        return false;
      } else if ($xml_response === false) {
      throw new WebToolkit_BingAssets_Exception('cURL no response for ' . $urlXmlBingAssetEndpoint);
        return false;
      }

      if(!$bingSearchAssetXml = simplexml_load_string($xml_response)) {
      throw new WebToolkit_BingAssets_Exception('Exception: cURL error occured during request for ' . $urlXmlBingAssetEndpoint . ': ' . curl_errno($curlHandle) . ' - ' . curl_error($curlHandle));
    return false;
      }

      $response = $bingSearchAssetXml->xpath($xPath);
      $response = (string) $response[0];
  } catch (WebToolkit_BingAssets_Exception $ex) {
    throw $ex;
  } catch (Exception $ex) {
    throw new WebToolkit_BingAssets_Exception('Unexpected Exception: ' . $ex->getMessage());
    return false;
  }

  return true;
}

?>